<?php

/**
 * brand actions.
 *
 * @package    virtualstore
 * @subpackage brand
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class brandActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->brands = Doctrine::getTable('Brand')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->brand = Doctrine::getTable('Brand')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->brand);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BrandForm();
    $ssn_user = sfContext::getInstance()->getUser();
    $a_id= $ssn_user->getGuardUser()->getId();

    $this->form->setDefault('created_by',$a_id);
    $this->form->setDefault('updated_by',array('value=>'.$a_id));
    //$this->form->setDefault('name','ddddddddd');
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BrandForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($brand = Doctrine::getTable('Brand')->find(array($request->getParameter('id'))), sprintf('Object brand does not exist (%s).', $request->getParameter('id')));
    $this->form = new BrandForm($brand);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($brand = Doctrine::getTable('Brand')->find(array($request->getParameter('id'))), sprintf('Object brand does not exist (%s).', $request->getParameter('id')));
    $this->form = new BrandForm($brand);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($brand = Doctrine::getTable('Brand')->find(array($request->getParameter('id'))), sprintf('Object brand does not exist (%s).', $request->getParameter('id')));
    $brand->delete();

    $this->redirect('brand/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $brand = $form->save();

      $this->redirect('brand/edit?id='.$brand->getId());
    }
  }
}
